CNC Example 02: Online Decoding with Variables
See the CNC02_online.project
sample project in the installation directory of CODESYS under ..\CODESYS SoftMotion\Examples
.
The example shows how to use variables to decode a CNC program.
Creating an NC program in the CNC editor
Create a
CNCOnline
program with a SoftMotion controller.Create a global variable list and declare two variables.
VAR_GLOBAL g_x: REAL:=100; g_y:REAL:=50; END_VAR
Insert a CNC program object named
Example
.Select the Implementation
Din66025
and the Compile modeSMC_CNC_REF
. This mode is required because you use variables in your program.Specify the following motion blocks:
CNC editor:
Creating a drive interface and PLC configuration
Define a drive structure (e.g. CNCdirect
).
Creating an IEC program
Add a POU (CFC) named
Path
to the application.The decoding of the NC program for OUTQUEUE and the velocity check take place in the
Path
program.Calling
SMC_CheckVelocities
is required.CFC:
Add a POU (CFC) named
Ipo
to the application.This program is almost identical to the
CNCdirect
sample project. However, the data input of the interpolator does not correspond to the CNC program names (ADR(Example)
), but to the OutQueue output of the path preprocessing function blocks (checkVel.poqDataOut
).
Creating a task for path preprocessing
Because you have selected the compile mode SMC_CNC_REF
, you have to perform decoding and path preprocessing in the IEC program. This calculation is time consuming. It does not have to be executed in the interpolator cycle because one path object is generated per decoder call, and this object is typically used for many interpolator calls. You should swap out this operation to a task with low priority and less frequent calls.
Create a
PathTask
task.Define the following parameters:
Priority: 10
Interval: T#30ms
Add the
Path
POU to the task.

In the slow task, approximately one GEOINFO object is generated per cycle at first. This object is stored in the OUTQUEUE structure of the decoder function block. If the OUTQUEUE is full, then the function blocks of the slow task pause until the OUTQUEUE is not full anymore. This happens as soon as the fast task processes the first GEOINFO object and deletes it from the OUTQUEUE.
Then the function blocks of the slow task become active again and fill the OUTQUEUE structure.
In the fast task, a path point from the OUTQUEUE structure, which the
DataIn
input points to, is calculated and processed in each cycle. Because a GEOINFO object generally consists of multiple path points, it takes a few cycles until the first GEOINFO object is processed and deleted automatically by the interpolator.As the processing of a GEOINFO object lasts several cycles as opposed to it creation, the slow task can be called less frequently than the fast task.
However, the task times have to be selected so that enough GEOINFO objects are always stored in the last OUTQUEUE of the slow task, thus preventing the occurrence of any data underrun. This happens when there are no more GEOINFO objects available to the interpolator from DataIn, and the path end has not been reached yet. In this case, the interpolator slows down and stops until new data elements are available again.
Creating an operating interface and a testing interface
The visualization corresponds to the CNCdirect.project
sample project. Add additional templates to the new function blocks (SMC_NCDecoder
and SMC_CheckVelocities
). Create a display also for the global variables g_x
and g_y
so that you can check their function later when commissioning.
Commissioning
Compile and start the created program. The program executes the CNC motion as soon as the Execute
input of the decoder and interpolator has been set. If you change the values of the global variables, then they are used when the decoder is restarted and the path is adapted accordingly. Monitor the function of the Append
input of the decoder as well.